package com.fingard.dsp.bank.directbank.gzrcb01;

import java.util.HashMap;
import java.util.Map;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.directbank.gzelink01.GZELINK01Base;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/5/10.
 */
public class GZRCB01Balance extends GZRCB01Base implements IBalance {
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        String opname = "YQ261";
        try {
            while (p_req7506.readRecord()) {

                    RetAtsBalDetail detail = null;

                    //非公有参数
                    XmlTextWriter xmlTextWriter = new XmlTextWriter();
                    GZRCB01Header header = new GZRCB01Header();
                    header.chnl_dt =Format.Date8Format.format(Format.DateTimeFormat.parse(p_req7506.reqHeader.transTime));
                    header.chnl_tm =Format.Time6Format.format(Format.DateTimeFormat.parse(p_req7506.reqHeader.transTime));
                    header.chnl_seq = p_req7506.reqHeader.transSeqID;
                    wirteCommenHeader(xmlTextWriter,header);
                    XElement main_data =new XElement("main_data","");
                    main_data.addChild(new XElement("USERID",bankFront.loginName));
                    main_data.addChild(new XElement("KEHHAO",bankFront.org));
                    main_data.addChild(new XElement("KEHUZH",p_req7506.readedDetail.actNum));
                    main_data.addChild(new XElement("NAOFAC","0000"));
                    main_data.addChild(new XElement("QISHBS","1"));
                    main_data.addChild(new XElement("CXUNBS","10"));
                    String plain = bankFront.loginName + "|" + bankFront.org + "|"+p_req7506.readedDetail.actNum;
                    main_data.addChild(new XElement("SIGDAT",plain));
                    xmlTextWriter.writeXElement(main_data);
                    xmlTextWriter.writeEndDocument();
                    String[] bankResult = sendToBank(xmlTextWriter.toXmlString(), opname);
                    if (bankResult[0].length() == 0) {
                        StringBuilder tmpSb = new StringBuilder();
                        XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSb);
                        if (tmpBkReader != null) {
                            String rsp_no = tmpBkReader.getFirstText("/msg/comm_head/rsp_no");
                            String rsp_msg = tmpBkReader.getFirstText("/msg/comm_head/rsp_msg");

                            if ("SUCCESS".equals(rsp_no) || "AAAAAAA".equals(rsp_no)) {
                                if (tmpBkReader.read("/msg/main_data/list_01/row")){
                                        detail = new RetAtsBalDetail();
                                        detail.respCode = "0";
                                        detail.respInfo = rsp_msg;
                                        detail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleText("HUOBDH"));
                                        detail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("KEHUZH");
                                        detail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("AVABAL");
                                        detail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("ZHHUYE");
                                        String lastDate = tmpBkReader.readedXElement.getSingleTextNotNull("LASTDT");
                                        if(com.fingard.text.StringHelper.hasAnyChar(lastDate)) {
                                            detail.lastTranDate =Format.Date10Format.format(Format.Date8Format.parse(lastDate));//待定
                                        }
                                }
                            } else {
                                detail = new RetAtsBalDetail();
                                detail.respCode = "-1";
                                detail.respInfo = rsp_msg;
                            }
                        } else {
                            detail = new RetAtsBalDetail();
                            detail.respCode = "-1";
                            detail.respInfo = tmpSb.toString();
                        }
                    } else {
                        detail = new RetAtsBalDetail();
                        detail.respCode = "-1";
                        detail.respInfo = bankResult[1];
                    }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
                }
        } catch (Exception e) {
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
